package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

// 这里填写你的openai api key
var GPTAPIKey = "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"

func M1() {
	// 设置代理服务器的地址
	//proxyUrl, err := url.Parse("http://qssmhrynxm6mynybhuhq.wgetapi.com:4032")
	//if err != nil {
	//	panic(err)
	//}
	//
	//// 创建一个HTTP Transport
	//transport := &http.Transport{
	//	Proxy: http.ProxyURL(proxyUrl),
	//}

	url := "https://api.openai.com/v1/chat/completions"
	method := "POST"

	payload := strings.NewReader(`{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "你是谁"
        }
    ]
}`)

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)
	//// 设置HTTP客户端的Transport
	//client.Transport = transport
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer 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")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

func M2() {
	// 设置代理服务器的地址
	proxyUrl, err := url.Parse("http://127.0.0.1:7890")
	if err != nil {
		panic(err)
	}

	// 创建一个HTTP Transport
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyUrl),
	}

	url := "https://api.openai.com/v1/chat/completions"
	method := "POST"

	payload := strings.NewReader(`{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "你是谁"
        }
    ]
}`)

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)
	// 设置HTTP客户端的Transport
	client.Transport = transport
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer 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")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}
